package com.laikan.reader.bookfriend.activity;

import android.graphics.Typeface;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import com.laikan.library.components.activitys.BasicActivity;
import com.laikan.library.httpvolley.engine.HttpApiRequestEngine;
import com.laikan.library.httpvolley.engine.Requester;
import com.laikan.library.httpvolley.helper.RequestListener;
import com.laikan.library.httpvolley.helper.VolleyErrorHandler;
import com.laikan.library.httpvolley.response.HttpResponse;
import com.laikan.library.managers.ToastMgr;
import com.laikan.library.utils.SPUtil;
import com.laikan.reader.R;
import com.laikan.reader.comment.APIProtocol;
import com.laikan.reader.core.utils.LoadingDialog;
import com.socks.library.KLog;

import java.util.HashMap;
import java.util.Map;

/**
 * 领取活动礼包
 */
public class FestivalCouponActivity extends BasicActivity implements OnClickListener {

    private TextView tv_receive;
    private TextView tv_cancel;
    private TextView tv_ticket;
    private TelephonyManager tm;
    private LoadingDialog mloadingDialog;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_receive:    // 领取
                receiveOrcancelFestival(1);
                SPUtil.putBoolean("isOpenFestival", false);
                exit();
                break;
            case R.id.tv_cancel:    // 取消
                receiveOrcancelFestival(2);
                SPUtil.putBoolean("isOpenFestival", false);
                exit();
                break;
        }
    }

    /**
     * 领取活动礼包
     *
     * @param i i==1: 领取; i==2: 取消
     */
    private void receiveOrcancelFestival(final int i) {
        mloadingDialog.show();
        Map<String, String> map = new HashMap<String, String>();
        map.put("sd", SPUtil.getString("token", ""));
        map.put("sequence", SPUtil.getString("sequence", ""));
        map.put("deviceId", tm.getDeviceId());
        map.put("recive", i + "");
        RequestListener requestListener = new RequestListener() {
            @Override
            public void onPreExecute() { KLog.e("---------volley onPreExecute!!!"); }

            @Override
            public void onSuccess(HttpResponse httpResponse) {
                mloadingDialog.dismiss();
                // 无返回信息
                if (i == 1) {
                    ToastMgr.showShortToast("领取成功!");
                }
            }

            @Override
            public void onError(VolleyErrorHandler errorHandler) {
                mloadingDialog.dismiss();
                ToastMgr.showShortToast(R.string.laikan_network_anomaly);
                KLog.e("87  网络不给力，请重试。 领取活动礼包与服务器连接失败");
            }

            @Override
            public void onFinish() {

            }
        };

        Requester requester = new Requester();
        requester.setUrl(APIProtocol.laikan_festivalcoupon);
        requester.setMapBody(map);
        HttpApiRequestEngine.getInstance().httpPostRequest(requester, requestListener);

//        HttpApiRequestEngine.getInstance().httpPostRequest(APIProtocol.laikan_festivalcoupon, map, null, null, requestListener);
    }

    private void exit() {
        new Handler() {
        }.postDelayed(new Runnable() {

            @Override
            public void run() {
                finish();
            }
        }, 500);
    }

    @Override
    protected void assignViews() {
        setContentView(R.layout.lk_festival_coupon);
        tv_receive = (TextView)findViewById(R.id.tv_receive);
        tv_cancel = (TextView)findViewById(R.id.tv_cancel);
        tv_ticket = (TextView)findViewById(R.id.tv_ticket);
        tv_ticket.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//加粗
        tv_ticket.getPaint().setFakeBoldText(true);//加粗
    }

    @Override
    protected void initData() {
        mloadingDialog = new LoadingDialog(mContext);

    }

    @Override
    protected void setListener() {
        tv_receive.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
            /*Intent intent0 = new Intent();
            intent0.putExtra("mes", "");
			setResult(RESULT_OK, intent0);*/
                exit();
                return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
